<template>
  <!--布局-->
  <el-container class="home-container">
    <!--头部布局-->
    <el-header>
      <div class="el-header-div">
        <img src="../assets/heima.png" />
        <span class="el-header-span">管理系统</span>
      </div>
      <el-button @click="logOut" type="info">退出</el-button>
    </el-header>
    <el-container>
      <!--左侧菜单布局-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!--导航组件-->
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#31A1FF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath">
          <!--一级菜单-->
          <el-submenu v-for="(item) in menulist" :key="item.id" :index="item.id + ''">

            <!--一级菜单模板区-->
            <template slot="title">
              <!--图标-->
              <i :class="iconsObj[item.id]"></i>
              <!--文本-->
              <span>{{ item.authName }}</span>
            </template>

            <!--二级菜单-->
            <el-menu-item :index="'/' + subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="saveNavStatus('/' + subItem.path)">
              <!--二级菜单模板区-->
              <template slot="title">
                <!--图标-->
                <i class="el-icon-s-grid"></i>
                <!--文本-->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
        <!--菜单向左折叠-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
      </el-aside>
      <!--右侧主体布局-->
      <el-main>
        <!--通过路由配置可以在/home重定向到/welcome-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      // 菜单图标，最好配置到数据库中
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠菜单
      isCollapse: false,
      // 左侧激活的菜单是哪个path
      activePath: ''
    }
  },
  methods: {
    logOut() {
      // 退出 先清空token 然后跳转到/login
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单列表
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 折叠、展开菜单
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存点击的菜单状态
    saveNavStatus (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  created () {
    this.getMenuList()
    // 获取存储的激活的path
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>
<!-- element的元素名称就是类名 -->
<style scoped>
.el-header{
  background-color: #363D40;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header-div{
  display: flex;
  align-items: center;
}
.el-header-span{
  padding-left: 15px;
}

.el-aside{
  background-color: #313743;
}
.el-menu{
  border-right: none;
}

.el-main{
  background-color: #E9EDF1;
}

.home-container{
  height: 100%;
}

.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.3em;
  cursor: pointer;
  position: fixed;
  left: 0;
  bottom: 5px;

}
</style>
